package training.minggu5.maven.service.impl;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import training.minggu5.maven.domain.PenjualanDetail;
import training.minggu5.maven.service.PenjualanDetailService;

@Service("penjualanDetailService")
@Transactional(readOnly=true)
public class PenjualanDetailServiceImpl implements PenjualanDetailService {

    @Autowired
    private SessionFactory sessionFactory;
    private String hqlFindAll = "from PenjualanDetail";
    private String hqlFindById = "from PenjualanDetail pd where pd.id_penjualan_detail = :id";
    private String hqlFindByKodeBarang = "from PenjualanDetail pd where pd.barang_kode_barang = :kd";
    private String hqlFindByNoFaktur = "";
/*
    @Override
    public void savePenjualanDetail(PenjualanDetail penjualan_detail) {
        sessionFactory.getCurrentSession().saveOrUpdate(penjualan_detail);
    }

    @Override
    public void deletePenjualanDetail(PenjualanDetail penjualan_detail) {
        sessionFactory.getCurrentSession().delete(penjualan_detail);
    }
*/
    @Override
    public List<PenjualanDetail> findAllPenjualanDetail() {
        return sessionFactory.getCurrentSession().createQuery(hqlFindAll).list();
    }

    @Override
    public PenjualanDetail findPenjualanDetailById(Integer id) {
        return (PenjualanDetail) sessionFactory.getCurrentSession().createQuery(hqlFindById)
                .setParameter("id", id).uniqueResult();
    }

    @Override
    public List<PenjualanDetail> findPenjualanDetailByKodeBarang(String kode_barang) {
        return sessionFactory.getCurrentSession().createQuery(hqlFindByKodeBarang)
                .setParameter("kd", kode_barang).list();
    }

    @Override
    public List<PenjualanDetail> findPenjualanDetailByNoFaktur(String no_faktur) {
        return sessionFactory.getCurrentSession().createQuery(hqlFindByNoFaktur)
                .setParameter("", no_faktur).list();
    }
    
}
